package com.youshang520i.thread;

import org.junit.Test;

import java.sql.Time;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author youshang
 * @date 2021/03/05 10:27
 **/
public class LockBlockingPractice {


    public static void main(String[] args) {

        Targe targe = new Targe();
        for (int i = 0; i < 10; i++) {
            BlockingProducer blockingProducer1 = new BlockingProducer(targe);
            Thread producer1 = new Thread(blockingProducer1, "producer1- " + i + ": ");
            producer1.start();
        }


        for (int i = 0; i < 10; i++) {
            BlockingConsumer blockingConsumer1 = new BlockingConsumer(targe);
            Thread Consumer1 = new Thread(blockingConsumer1, "Consumer" + i + "-1 : ");
            Consumer1.start();
        }

    }
}


class BlockingProducer implements Runnable {
    private Targe targe;

    public BlockingProducer(Targe targe) {
        this.targe = targe;
    }

    @Override
    public void run() {
        while (true) {
            targe.add();
        }
    }
}

class BlockingConsumer implements Runnable {
    private Targe targe;

    //这个参数
    public BlockingConsumer(Targe targe) {
        this.targe = targe;
    }

    @Override
    public void run() {
        while (true) {
            targe.remove();
        }
    }
}

/**
 * 核心方法 当前方法不能再多线程中通过new的方式创建，只能通过构造函数的形式去获取当前对象
 */
class Targe {
    //一定需要确认当前多线程中的参数是一致的
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private BlockingQueue blockingQueue = new LinkedBlockingQueue();


    public void add() {
        lock.lock();
        try {
            while (blockingQueue.size() == 2) {
                try {
                    System.out.println("wait add : " + Thread.currentThread().getName());
                    condition1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            blockingQueue.add(Thread.currentThread().getName() + blockingQueue.size());
            System.out.println("添加： " + Thread.currentThread().getName() + blockingQueue.size());
            Thread.sleep(200);
            condition2.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void remove() {
        lock.lock();
        try {
            while (blockingQueue.size() == 0) {
                try {
                    System.out.println("wait remove : " + Thread.currentThread().getName());
                    condition2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            blockingQueue.poll(2, TimeUnit.SECONDS);
            String str = Thread.currentThread().getName() + blockingQueue.size();
            System.out.println("移除： " + str);
            Thread.sleep(500);
            condition1.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

